/**
 * Created by Administrator on 2017/9/17.
 */

import bean.Formula;
import bean.Number;

import java.io.*;
import java.lang.System;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        System.out.println("请问要进行什么操作");
        System.out.println("1.生成题目");
        System.out.println("2.计算结果");
        try {
            Scanner input = new Scanner(System.in);
            Object number = input.nextInt();
            if (number.equals(1)) {
                System.out.println("请输入生成题目数量");
                Scanner input2 = new Scanner(System.in);
                Object inputNumber1 = input2.nextInt();
                Integer n = (Integer) inputNumber1;
                System.out.println("请输入自然数范围");
                Scanner input3 = new Scanner(System.in);
                Object inputNumber2 = input3.nextInt();
                Integer r = (Integer) inputNumber2;
                //清空题目文件
                File exercisesFile = new File("file/Exercises.txt");
                if (exercisesFile.exists()) {
                    exercisesFile.delete();
                }
                //清空答案文件
                File answersFile = new File("file/Answers.txt");
                if (answersFile.exists()) {
                    answersFile.delete();
                }
                //清空试题文件
                File yourFile = new File("file/YourFile.txt");
                if (yourFile.exists()) {
                    yourFile.delete();
                }
                for (int i = 0; i < n; i++) {
                    Formula formula = new Formula(r);
                    String aFormula = formula.getAFormula();
                    FileWriter exercisesFw = new FileWriter(exercisesFile, true);
                    BufferedWriter exercisesBw = new BufferedWriter(exercisesFw);
                    exercisesBw.write((i + 1) + "." + aFormula);
                    exercisesBw.newLine();
                    exercisesBw.flush();
                    exercisesBw.close();
                    FileWriter answersFw = new FileWriter(answersFile, true);
                    BufferedWriter answersBw = new BufferedWriter(answersFw);
                    Number answer = formula.getAnswser();
                    answersBw.write((i + 1) + "." + answer.toString());
                    answersBw.newLine();
                    answersBw.flush();
                    answersBw.close();
                    FileWriter yourFileFw = new FileWriter(yourFile, true);
                    BufferedWriter yourFileBw = new BufferedWriter(yourFileFw);
                    yourFileBw.write((i + 1) + ".");
                    yourFileBw.newLine();
                    yourFileBw.flush();
                    yourFileBw.close();
                }
                System.out.println("生成成功，请在ExercisesFile.txt文件中查看");
            } else if (number.equals(2)) {
                System.out.println("计算结果中");
                //删除Grade文件
                File gradeFile = new File("file/Grade.txt");
                if (gradeFile.exists()) {
                    gradeFile.delete();
                }
                //定义答案文件和答题文件的输入流
                File answersFile = new File("file/Answers.txt");
                FileReader answersRd = new FileReader(answersFile);
                BufferedReader answersBufferRd = new BufferedReader(answersRd);
                File yourFile = new File("file/YourFile.txt");
                FileReader yourFileRd = new FileReader(yourFile);
                BufferedReader yourFileBufferRd = new BufferedReader(yourFileRd);
                String thisLineAnswer = "";
                String yourAnswer = "";
                List<Integer> error = new ArrayList<Integer>();
                List<Integer> right = new ArrayList<Integer>();
                int num = 1;
                while ((thisLineAnswer = answersBufferRd.readLine()) != null) {
                    yourAnswer = yourFileBufferRd.readLine();
                    if (yourAnswer.equals(thisLineAnswer)) {
                        right.add(num);
                    } else {
                        error.add(num);
                    }
                    num++;
                }
                //关闭流
                answersBufferRd.close();
                yourFileBufferRd.close();

                //将答题结果写入到Grade文件中
                FileWriter gradeFw = new FileWriter(gradeFile, true);
                BufferedWriter gradeBw = new BufferedWriter(gradeFw);
                gradeBw.write("Correct:"+right.size()+"(");
                for (int x=0;x<right.size();x++){
                    gradeBw.write(right.get(x)+",");
                }
                gradeBw.write(")");
                gradeBw.newLine();
                gradeBw.write("Wrong:"+error.size()+"(");
                for (int y=0;y<error.size();y++){
                    gradeBw.write(error.get(y)+",");
                }
                gradeBw.write(")");
                gradeBw.newLine();
                //关闭流
                gradeBw.flush();
                gradeBw.close();
                System.out.println("结算成功，请在grade.txt文件中查看");
            } else {
                System.out.println("输入正确的数值");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
